package bd

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import oa.lla.cca.R
import oa.lla.cca.databinding.ActivitySdsourcesBinding
import java.util.*


/**
 * 获取SKD图片和视频资源
 */
class CI : AppCompatActivity() {
    private var databind: ActivitySdsourcesBinding? = null
    private var permissionList = Arrays.asList<String>(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
    private var mFragments: ArrayList<Fragment> = ArrayList<Fragment>()
    private var viewModule:FI? = null
    private var fragmentTag = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        databind = ActivitySdsourcesBinding.inflate(layoutInflater)
        setContentView(databind!!.root)
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, permissionList.toTypedArray(),100)
        }else{
            initView()
        }
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100){
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                initView()
            }else{
                finish()
            }
        }

    }



    private fun initView() {
        viewModule = ViewModelProvider(this).get(FI::class.java)
        databind?.apply {
            viewModule?.getSDSources()
            backLeft.setOnClickListener { finish() }
            fragmentTag = FD.newInstance()::class.java.simpleName
            showFragment(FD.newInstance())
            selectImgBt.setOnClickListener {
                fragmentTag = FD.newInstance()::class.java.simpleName
                showFragment(FD.newInstance())
                selectImgBt.setTextColor(Color.WHITE)
                selectImgBt.setBackgroundResource(R.drawable.pic_select_true_shap)
                selectVideoBt.setTextColor(Color.parseColor("#979797"))
                selectVideoBt.setBackgroundResource(R.drawable.video_select_default_shap)
            }
            selectVideoBt.setOnClickListener {
//                fragmentTag = VideoSourceFragment.newInstance()::class.java.simpleName
//                showFragment(VideoSourceFragment.newInstance())
//                selectVideoBt.setTextColor(Color.WHITE)
//                selectVideoBt.setBackgroundResource(R.drawable.video_select_true_shap)
//                selectImgBt.setTextColor(Color.parseColor("#979797"))
//                selectImgBt.setBackgroundResource(R.drawable.pic_select_default_shap)
            }
        }
    }


    private fun showFragment(fragment: Fragment?) {
        hideFragments()
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        if (fragment != null && mFragments.contains(fragment)) {
            fragmentTransaction.show(fragment)
        } else {
            if (fragment != null && !fragment.isAdded) {
                fragmentTransaction.add(R.id.source_content_layout, fragment,fragmentTag)
                mFragments.add(fragment)
            }
        }
        fragmentTransaction.commitNowAllowingStateLoss()
    }

    private fun hideFragments() {
        if (mFragments.size > 0) {
            for (fragment in mFragments) {
                val fragmentTransaction = supportFragmentManager.beginTransaction()
                fragmentTransaction.hide(fragment)
                fragmentTransaction.commitNowAllowingStateLoss()
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        viewModule?.imageSourceLiveData?.removeObservers(this)
        viewModule?.videoSourceLiveData?.removeObservers(this)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data!=null){
            val pageType = data.getStringExtra(CH.PAGE_TYPE)
            if (pageType.equals(CH.PAGE_IMAGE)){
                val fragment1 = supportFragmentManager.findFragmentByTag(
                    FD::class.java.simpleName
                )
                fragment1?.onActivityResult(requestCode, resultCode,data)
            }else{
//                val fragment1 = supportFragmentManager.findFragmentByTag(
//                    VideoSourceFragment::class.java.simpleName
//                )
//                fragment1?.onActivityResult(requestCode, resultCode,data)
            }
        }

    }




}